package handler

import (
	"class/api/request"
	"class/glodal"
	"class/inits"
	"class/model"
	"class/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func Dan(c *gin.Context) {
	var req request.Dan
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "参数错误",
		})
		return
	}
	get, _ := glodal.Rdb.Get(inits.Ctx, "content").Int()
	if get >= 3 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "发送的弹幕违规超过三次禁止发送",
		})
		glodal.Rdb.Set(inits.Ctx, "message", get, time.Minute*30)
		return
	}

	res := pkg.Content(req.Content)
	dan := model.Dan{
		Content: req.Content,
	}
	if res {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"message": "发送成功",
		})
		glodal.DB.Create(&dan)
		return
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "内容不合规",
		})
		glodal.Rdb.Incr(inits.Ctx, "content")
		return
	}
}

func MessageList(c *gin.Context) {

	var message []model.Message
	glodal.DB.Find(&message)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": message,
	})
}
